package emulator.util.file;

import java.io.FileOutputStream;
import java.io.IOException;

import emulator.hardware.memory.MemoryBlock;
import emulator.shell.DebugShell;

public class FileSaveStrategyPrg implements FileSaveStrategy {

	@Override
	public void saveFromMemory(String file_name, MemoryBlock memory, int start_address, int block_size) {
		FileOutputStream file = null;
		try
		{
			file = new FileOutputStream(file_name);
			file.write(start_address & 0xFF);
			file.write((start_address >> 8) & 0xFF);
			memory.saveToStream(file, start_address, block_size);
		}
		catch (IOException e)
		{
			DebugShell.getInstance().getErr().println("File write failed for \""+file_name+"\" (raw): "+e.getMessage());
		}
		finally
		{
			if (file != null)
				try { file.close(); } catch (IOException e) {}
		}
	}

}
